﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class OccupationDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_OCCUPATION = "spGetListPagingOccupation";
        public const string SP_GET_LIST_PAGING_OCCUPATION_BY_ID = "spGetOccupationById";
        public const string SP_CREATE_OR_UPDATE_OCCUPATION = "spCreateOrUpdateOccupation";
        public const string SP_DELETE_OCCUPATION = "spDeleteOccupation";
        public const string SP_GET_ALL_OCCUPATION = "spGetAllOccupation";
        
        public const string Param_OCCUPATION_ID = "@OCCUPATION_ID";
        public const string Param_NAME = "@NAME";
        public const string Param_CODE = "@CODE";
        public const string Param_DELETED = "@DELETED";

        public OccupationDAO(String connectionString, System.Type type = null)
            : base(connectionString, type)
        {

        }

        internal DataPage<OccupationModel> GetDynamicListPaging(OccupationModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_OCCUPATION);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_NAME, "%" + model.NAME + "%"));
                    command.Parameters.Add(CreateParameter(Param_CODE, "%" + model.CODE + "%"));
                    command.Parameters.Add(CreateParameter(Param_DELETED, model.DELETED));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<OccupationModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal OccupationModel GetById(string occupation_Id, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_OCCUPATION_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_OCCUPATION_ID, occupation_Id));

                return GetSingle<OccupationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateOccupation(OccupationModel OccupationModel)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_OCCUPATION);
                command.Parameters.Add(new SqlParameter("@" + OccupationModel.FIELD_OCCUPATION_ID, OccupationModel.OCCUPATION_ID));
                command.Parameters.Add(new SqlParameter("@" + OccupationModel.FIELD_NAME, OccupationModel.NAME.Trim()));
                command.Parameters.Add(new SqlParameter("@" + OccupationModel.FIELD_CODE, OccupationModel.CODE.Trim()));
                command.Parameters.Add(new SqlParameter("@" + OccupationModel.FIELD_PARENT, OccupationModel.PARENT.Trim()));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteOccupation(string[] occupation_Id)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_OCCUPATION);
                command.Parameters.Add(new SqlParameter("@TableId", GetTableId<string>(occupation_Id)));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal List<OccupationModel> GetAll(Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_OCCUPATION);
                return GetList<OccupationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }
    }
}
